package com.snail.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.snail.model.dto.QuestionMultiCollectDto;
import com.snail.model.pojo.BizQuestion;
import com.snail.model.pojo.BizQuestionCollect;
import com.snail.response.BaseResponse;
import com.snail.response.ResultUtils;
import com.snail.service.BizQuestionCollectService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ysc
 * @descrpiton 问题收藏处理
 * @date 2025/7/22 11:47
 **/
@RestController
@RequestMapping("/questionCollect")
public class BizQuestionCollectionController {


    @Autowired
    private BizQuestionCollectService bizQuestionCollectService;

    /**
     * 收藏题目到收藏夹
     */
    @Operation(summary = "收藏到单个文件夹")
    @PostMapping("/add")
    public BaseResponse<?> add(@RequestBody BizQuestionCollect collect) {
        try {
            boolean success = bizQuestionCollectService.addQuestionToCollect(collect);
            return success ? ResultUtils.success("收藏成功") : ResultUtils.error("收藏失败");
        } catch (RuntimeException e) {
            return ResultUtils.error(e.getMessage());
        }
    }
    /**
     * 收藏题目到多个收藏夹
     */
    @Operation(summary = "收藏到多个文件夹")
    @PostMapping("/addMulti")
    public BaseResponse<?> addMulti(@RequestBody QuestionMultiCollectDto request) {
        try {
            Integer questionId = request.getQuestionId();
            List<Integer> folderIds = request.getFolderId();

            if (questionId == null || folderIds == null || folderIds.isEmpty()) {
                return ResultUtils.error("题目或收藏夹不能为空");
            }

            for (Integer folderId : folderIds) {
                BizQuestionCollect collect = new BizQuestionCollect();
                collect.setQuestionId(questionId);
                collect.setCollectId(folderId);
                //collect.setUserId(StpUtil.getLoginIdAsInt());
                bizQuestionCollectService.addQuestionToCollect(collect);
            }

            return ResultUtils.success("题目已收藏到多个收藏夹");
        } catch (Exception e) {
            return ResultUtils.error("收藏失败：" + e.getMessage());
        }
    }
    /**
     * 从收藏夹中移除题目
     */
    @DeleteMapping("/delete")
    public BaseResponse<?> delete(@RequestParam Integer questionId, @RequestParam Integer collectId) {
        boolean success = bizQuestionCollectService.removeQuestionFromCollect(questionId, collectId);
        return success ? ResultUtils.success("取消收藏成功") : ResultUtils.error("取消收藏失败");
    }

    /**
     * 查询收藏夹中所有题目
     */
    @GetMapping("/questions/{collectId}")
    public BaseResponse<?> getQuestionsInCollect(@PathVariable Integer collectId) {
        List<BizQuestion> list =bizQuestionCollectService.getQuestionsInCollect(collectId);
        return ResultUtils.success(list);
    }
}
